#include "pch.h"
#include "para.h"
#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "solver.h"

#pragma warning(disable:4273)

using namespace std;

const int N = 1e6 + 10, M = 2e5 + 10;
char *result[N];
char *words[M];

int solve(int argc, char* argv[]) {
    int cal_type, head = 0, tail = 0, banned = 0;
    int loop_enable = 0;
    char* path;
    //int parse_para(int argc, char *argv[], int &cal_type, int &head, int &tail,
    //               int &banned, int &loop, char **path)
//    cout << "begin parse_para..." << endl;
    int r = parse_para(argc, argv, cal_type, head, tail,banned , loop_enable,&path);
    if (r < 0) {
        return r;
    }
//    cout << "finish parse_para!" << endl;

    static int wordCount = 0;
//    cout << "begin getWords..." << endl;
    r = getWords(path, words, &wordCount);
//    cout << "finish getWords!" << endl;

    if (r < 0) {
        return r;
    }
    int tot = 0;
//    cout << "begin solve..." << endl;
    switch (cal_type) {
        case 1:
//            cout << "start" << endl;
            tot = gen_chains_all(words, wordCount, result);
            if (tot < 0) {
                cerr << "loop exist" << endl;
                return tot;
            }
            outputRes(argv[0], tot, result, tot, false);
            break;
        case 2:
            tot = gen_chain_word(words,wordCount,result,head,tail,banned,loop_enable);
            if (tot < 0) {
                return tot;
            }
            outputRes(argv[0], tot, result, tot, true);
            break;
        case 3:
            tot = gen_chain_char(words,wordCount,result,head,tail,banned,loop_enable);
            if (tot < 0) {
                return tot;
            }
            outputRes(argv[0], tot, result, tot, true);
            break;
        default:
            break;
    }
    return 0;
//    cout << "finish solve!" << endl;
}